﻿
@section scripts {
    <script src="~/Scripts/metadata.js"></script>
    <script src="~/Scripts/manager.js"></script>
    <script type="text/javascript">
        var viewModel = {
            entityarray: ko.observableArray()
    };

    var serviceName = '/breeze/@ViewBag.Service';
    var manager = new createEntityManager(serviceName);

    $(function () {
        @*manager.fetchEntityByKey("@ViewBag.Type", "@ViewBag.Id").then(function (result) {
            var entityKey = result.entityKey;
            var fromCache = result.fromCache;
            viewModel.entityarray.push(result.entity);

            alert(viewModel.entityarray()[0].ClientId());
            alert(viewModel.entityarray()[0].StudyTime());
            alert(viewModel.entityarray()[0].Conversations()[0].Content);

            //alert($.isArray(viewModel.entityarray()[0].Conversations));


            ko.applyBindings(viewModel);
        }).fail(function (error) { alert(error); });*@

        var query = new breeze.EntityQuery().from("Clients");
        manager.executeQuery(query).then(function (data) {
            data.results.forEach(function (c) {
                viewModel.entityarray.push(c);
            });
            ko.applyBindings(viewModel);
        }).fail(function (e) {
            alert(e);
        });

        //Query person for add client
        var query2 = new breeze.EntityQuery().from("Persons");
        manager.executeQuery(query2).then(function (data) {
            data.results.forEach(function (c) {
                viewModel.entitypeople.push(c);
            });
        }).fail(function (e) {
            alert(e);
        });
    });

        


        function AddChild(type, parentindex) {
            var todoType = manager.metadataStore.getEntityType(type);
            var newTodo = todoType.createEntity();
            var ent = type + "s";
            viewModel.entityarray()[parentindex][ent].push(newTodo);
            //alert(viewModel.entityarray()[parentindex][ent]());
        }
        function Del(index, parentindex, type) {
            var ent = type + "s";
            var ItemtoRemove = viewModel.entityarray()[parentindex][ent]()[index];
            ItemtoRemove.entityAspect.setDeleted();
        }
        function AddParent() {
            var todoType = manager.metadataStore.getEntityType('Client');
            var newTodo = todoType.createEntity();
            viewModel.entityarray.push(newTodo);
            alert(viewModel.entityarray());
            //**********************************
            manager.addEntity(newTodo);
            //**********************************
        }

        function AddPerson_Client() {
            var todoType = manager.metadataStore.getEntityType('Person');
            var newTodo = todoType.createEntity({Gender : 0, MaritalStatus : 0 , MissingDoBParts : 0});
            viewModel.entitypeople.push(newTodo);           
            manager.addEntity(newTodo);

            var length = viewModel.entitypeople().length;
            var newpersonId = viewModel.entitypeople()[length - 1].SysId();

            var todoType2 = manager.metadataStore.getEntityType('Client');
            var newTodo2 = todoType2.createEntity({ ClientId: newpersonId });
            viewModel.entityarray.push(newTodo2);
            manager.addEntity(newTodo2);

        }

        function failed(error) {
            var ItemtoError = viewModel.entitypeople()[1];
            var err = ItemtoError.entityAspect.getValidationErrors()[0].errorMessage;
            alert(err);
        }
        function Save() {
            if (manager.hasChanges()) {
                manager.saveChanges().fail(failed).then(function () {
                    alert("Saved :)");

                });
            } else {
                alert("Nothing to save");
            };
        }

    </script>
}


@{
    string type = ViewBag.Type;
}
@Html.Partial("Entity/" + type)
